package com.coder.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.interceptor.SessionAware;

import com.coder.model.DayIsa;
import com.coder.model.Isa;
import com.coder.model.IsaAvg;
import com.coder.model.MonthIsa;
import com.coder.service.IsaService;
import com.coder.utils.DateUtilMy;
import com.coder.zexception.ServerErrorException;
import com.opensymphony.xwork2.ActionSupport;

public class IsaAction extends ActionSupport  implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	
	private String from;
	private String to;
	
	/**用于接收获取某一天的平均isa时的日期*/
	private String day;
	
	/**用于获取某一月的平均isa时的日期*/
	private String month;
	
	private IsaService isaService;
	
    /**Struts2中Map类型的session*/
    private Map<String, Object> session;
    
    /**以jsonArray的形式返回*/
    private String isasJson;
	protected JSONObject jsonObject = new JSONObject();
    
    /**获取平均isa数据的时间间隔，以分钟为单位*/
    private int interval;

    /**获取某一月的平均值*/
    public String getMonthAvgIsaData(){
    	
    	List<MonthIsa> monthIsas = null;
    	try {
    		monthIsas = isaService.getMonthIsaByMonth(month);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
		if(monthIsas==null){
			System.out.println("没有数据");
			isasJson = "";
			return SUCCESS;
		}
    	
		JsonConfig config = new JsonConfig();
        config.setIgnoreDefaultExcludes(false);
        
        //设置忽略的字段
        config.setExcludes(new String[]{"avgValStr","maxValStr","minValStr"});
 
        JSONArray array = new JSONArray();
        array = JSONArray.fromObject(monthIsas,config);
        isasJson = array.toString();
        
		System.out.println(isasJson);
		
    	return SUCCESS;
    }
    
    /**获取某一天的平均值*/
    public String getDayAvgIsaData(){
    	
    	Date dayOfIsa = null;
    	List<DayIsa> dayIsas = null;
    	try {
			dayOfIsa = DateUtilMy.dayFormat.parse(day);
			dayIsas = isaService.getDayIsaByDay(dayOfIsa);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
		if(dayIsas==null){
			System.out.println("没有数据");
			isasJson = "";
			return SUCCESS;
		}
    	
		JsonConfig config = new JsonConfig();
        config.setIgnoreDefaultExcludes(false);
        
        //设置忽略的字段
        config.setExcludes(new String[]{"avgValStr","maxValStr","minValStr","day"});
 
        JSONArray array = new JSONArray();
        array = JSONArray.fromObject(dayIsas,config);
        isasJson = array.toString();
        
		System.out.println(isasJson);
		
    	return SUCCESS;
    }
    
    /**获取一定时间间隔的Isa平均数据*/
	public String getAvgIsaData(){
		
		if(from.equals("") || to.equals("")){
			(new ServerErrorException("开始时间或结束时间不能为空")).printStackTrace();
			return ERROR;
		}
		
		if(interval<1){
			(new ServerErrorException("取平均值的时间间隔有误")).printStackTrace();
			return ERROR;
		}
		
		String[] fromDateTime = from.split(" ");
		String[] toDateTime = to.split(" ");

		List<IsaAvg> isaAvgs = null;
		
		//第一个参数表示日期，具体到天，第二个参数表示该天的超过阈值字符串，具体可以为(单位天数不带0)：
		//2014_9_11:12: 200 20 value,
		TreeMap<String,List<String>> waveAlerm  = new TreeMap<String,List<String>>();
		
		try{
			isaAvgs = isaService.getAverageData(fromDateTime[0],fromDateTime[1],
					toDateTime[0],toDateTime[1],interval,waveAlerm);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		for(String date:waveAlerm.keySet()){
			System.out.print("#######");
			System.out.print(date+": ");
			System.out.println(waveAlerm.get(date));
		}
		
		//有超过阈值的波长
		if(waveAlerm.size()>0){
			for(String date:waveAlerm.keySet()){
				jsonObject.put("firstMonth", date);
				break;
			}
		}else{
			jsonObject.put("firstMonth", "none");
		}
		
		if(isaAvgs==null){
			System.out.println("没有数据");
			jsonObject.put("isas", "nodata");
			return SUCCESS;
		}
		
		//TODO 显示的最多记录数，需要调整
		if(isaAvgs.size()>1000){
			System.out.println("数据超过1000条:"+isaAvgs.size());
			jsonObject.put("isas", "toomany");
			return SUCCESS;
		}
		
		JsonConfig config = new JsonConfig();
        JSONArray array = new JSONArray();
        array = JSONArray.fromObject(isaAvgs,config);
        isasJson = array.toString();
        
		System.out.println(isasJson);
		
		jsonObject.put("isas", isasJson);
		jsonObject.put("alarm", JSONObject.fromObject(waveAlerm));
        
		return SUCCESS;
	}
    
    
    /**获取一段时间的裸的Isa数据，Isa数据不经处理*/
	public String getIsaData(){
		
		if(from=="" || to==""){
			(new ServerErrorException("开始时间或结束时间不能为空")).printStackTrace();
			return ERROR;
		}
		
		String[] fromDateTime = from.split(" ");
		String[] toDateTime = to.split(" ");

		List<Isa> isas = null;
		try{
			isas = isaService.getIsaData(fromDateTime[0],fromDateTime[1],toDateTime[0],toDateTime[1]);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(isas==null){
			System.out.println("没有数据");
			isasJson = "";
			return SUCCESS;
		}
		
		//显示的最多记录数，需要调整
		if(isas.size()>100){
			System.out.println("数据太多");
			System.out.println(isas.size());
			isasJson = "toomany";
			return SUCCESS;
		}
		
		JsonConfig config = new JsonConfig();
        config.setIgnoreDefaultExcludes(false);
        
        //设置忽略的字段
        config.setExcludes(new String[]{"isakey","Data","CDate","CTime"});
 
        JSONArray array = new JSONArray();
        array = JSONArray.fromObject(isas,config);
        isasJson = array.toString();
        
		System.out.println(isasJson);
        
		return SUCCESS;
	}
	
	
	public Map<String, Object> getSession() {
		return session;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;		
	}

	public IsaService getIsaService() {
		return isaService;
	}

	public void setIsaService(IsaService isaService) {
		this.isaService = isaService;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}


	public String getIsasJson() {
		return isasJson;
	}


	public void setIsasJson(String isasJson) {
		this.isasJson = isasJson;
	}



	public int getInterval() {
		return interval;
	}



	public void setInterval(int interval) {
		this.interval = interval;
	}


	public String getDay() {
		return day;
	}


	public void setDay(String day) {
		this.day = day;
	}


	public String getMonth() {
		return month;
	}


	public void setMonth(String month) {
		this.month = month;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

}
